package com.sakiko.dailynote.validations;

import com.sakiko.dailynote.annotations.State;
import jakarta.validation.ConstraintValidator;
import jakarta.validation.ConstraintValidatorContext;

// 自定义注解的实践类
// <目标注解，校验类型>
public class StateValidation implements ConstraintValidator<State,String> {

    @Override
    public void initialize(State constraintAnnotation) {
        ConstraintValidator.super.initialize(constraintAnnotation);
    }

    /**
     *
     * @param s Data to be validated in the future
     * @param constraintValidatorContext context in which the constraint is evaluated
     * @return false: not approved  true: approved
     */
    @Override
    public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext) {
        // 提供校验规则
        if (s == null){
            return false;
        }
        if (s.equals("已发布") || s.equals("草稿")){
            return true;
        }
        return false;
    }
}
